﻿
//依赖于地图容器
var CustomMap = {
    //ModifyMapInit 加载修改状态的地图时，加载此函数
    Map: "",
    CenterPointX: 116.404, //中心轴的X坐标
    CenterPointY: 39.915, //中心轴的Y坐标
    Zoom: 12,    //地图的缩放比例
    MarkerPointX: 116.404, //标记的X坐标
    MarkerPointY: 39.915, //标记的Y坐标
    Init: function () {
        if (CustomMap.ModifyMapInit != null) {
            CustomMap.ModifyMapInit();
        }

        //创建一个坐标
        var a = new BMap.Point(CustomMap.CenterPointX, CustomMap.CenterPointY);
        //中心轴定位
        CustomMap.Map.centerAndZoom(a, CustomMap.Zoom);
        //滚动特效
        CustomMap.Map.enableScrollWheelZoom();
        //添加控件
        CustomMap.Map.addControl(new BMap.NavigationControl());

        //创建标记坐标
        var markPoint = new BMap.Point(CustomMap.MarkerPointX, CustomMap.MarkerPointY);
        //创建备注标记
        var b = new BMap.Marker(markPoint);
        b.enableDragging();
        b.addEventListener("dragend", function (e) {
            CustomMap.MarkerPointX = e.point.lng;
            CustomMap.MarkerPointY = e.point.lat;
        });
        CustomMap.MarkerPointX = markPoint.lng;
        CustomMap.MarkerPointY = markPoint.lat;
        var c = new BMap.Label("拖动改变位置", {
            offset: new BMap.Size("13", "-2")
        });

        b.setLabel(c);
        var d = new BMap.Icon("/Scripts/Map/img/location.gif", new BMap.Size(14, 23));
        b.setIcon(d);
        CustomMap.Map.clearOverlays();
        CustomMap.Map.addOverlay(b)


    },
    /*
    搜索地址并作地图定位
    @addressType 地址类型：0省,1是市,2县级 或 地区
    @addressInfo 需要搜索的地址信息
    */
    SearchAddress: function (addressType, addressInfo) {

        //缩放比例
        var proportion = 14;
        if (addressType == 0) {
            proportion = 8;
        } else if (addressType == 1) {
            proportion = 10;
        } else if (addressType == 2) {
            proportion = 12;
        }

        var e = new BMap.Geocoder();
        e.getPoint(addressInfo,
        function (n) {
            if (n != null) {
                var o = new BMap.Marker(n);
                o.enableDragging();
                var p = new BMap.Label("拖动改变位置", {
                    offset: new BMap.Size("13", "-2")
                });
                o.setLabel(p);
                var q = new BMap.Icon("/Scripts/Map/img/location.gif", new BMap.Size(14, 23));
                o.setIcon(q);
                CustomMap.MarkerPointX = n.lng;
                CustomMap.MarkerPointY = n.lat;
                CustomMap.Map.clearOverlays();
                CustomMap.Map.addOverlay(o);
                CustomMap.Map.centerAndZoom(n, proportion);
            } else {
                alert("无法定位，请地图中手动定位");
            }
        });
    }

};

CustomMap.Map = new BMap.Map("map_container");


(function () {
    CustomMap.Map.onclick = function (d) {
        var a = new BMap.Marker(d.point);
        a.enableDragging();
        a.addEventListener("dragend", function (e) {
            CustomMap.MarkerPointX = e.point.lng;
            CustomMap.MarkerPointY = e.point.lat;
        });
        var b = new BMap.Label("拖动改变位置", {
            offset: new BMap.Size("13", "-2")
        });
        a.setLabel(b);
        var c = new BMap.Icon("/Scripts/Map/img/location.gif", new BMap.Size(14, 23));
        a.setIcon(c);
        CustomMap.MarkerPointX = d.point.lng;
        CustomMap.MarkerPointY = d.point.lat;
        CustomMap.Map.clearOverlays();
        CustomMap.Map.addOverlay(a)
    }
})();
